package com.fqcar.fqdf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.baidu.location.BDLocation;
import com.fqcar.fqdf.DFApp;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.adapter.CarListAdapter;
import com.fqcar.fqdf.core.SwipLoadMoreActivity;
import com.fqcar.fqdf.entity.CarDetailEntity;
import com.fqcar.fqdf.entity.CarListReqBean;
import com.fqcar.fqdf.entity.CityEntity;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqcar.fqdf.util.ReqConstants;
import com.fqdf.framework.android.net.response.MidListRespBean;
import com.fqdf.framework.android.net.response.NetPageInfoBeanWrapper;
import com.fqdf.framework.android.util.Constants;
import com.fqdf.framework.android.view.LoadMoreListView;
import com.fqdf.framework.android.view.TwoSwipeRefreshLayout;
import com.fqdf.framework.android.view.listener.RecycleItemClickListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;


public class CarListActivity extends SwipLoadMoreActivity{
    @BindView(R.id.no_data_layout)
    NestedScrollView noDataLayout;
    @BindView(R.id.list)
    LoadMoreListView list;
    @BindView(R.id.swiprefresh)
    TwoSwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.main_car_count)
    TextView mainCarCount;
    @BindView(R.id.main_select_condition_btn)
    Button mainSelectConditionBtn;
    @BindView(R.id.main_back_iv)
    ImageView mainBackIv;
    @BindView(R.id.main_city_name_tv)
    TextView mainCityNameTv;
    @BindView(R.id.main_city_name_total_count_tv)
    TextView mainCityNameTotalCountTv;
    @BindView(R.id.main_city_name_layout)
    LinearLayout mainCityNameLayout;
    @BindView(R.id.main_search_btn)
    Button mainSearchBtn;

    private CarListAdapter adapter;

    private MidListRespBean carRespBean;

    private BDLocation location;

    private CarListReqBean carListReqBean;

    //用户选择的城市
    private CityEntity userCityEntity;

//    private CarListDetailEntity carListDetailEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_list);
        ButterKnife.bind(this);

        initData();
        initMyView();
    }

    private void initData() {
//        location = getIntent().getParcelableExtra(Constants.BUNDLE_DATA);
        if (getIntent().getParcelableExtra(Constants.BUNDLE_DATA) != null) {
            userCityEntity = getIntent().getParcelableExtra(Constants.BUNDLE_DATA);
        }
        location = ((DFApp) getApplication()).getCurLocation();
        mainCityNameTv.setText(userCityEntity == null ? location.getCity() : userCityEntity.getName());
        carListReqBean = new CarListReqBean();

        if(getIntent().getParcelableExtra(Constants.BUNDLE_DATA_EXT) != null){
            carListReqBean = getIntent().getParcelableExtra(Constants.BUNDLE_DATA_EXT);
        }
    }

    private void initMyView() {

        adapter = new CarListAdapter(this);
        list.setAdapter(adapter);
        initSwipLoadMoreView(list, mSwipeRefreshLayout, noDataLayout);
        // 进行刷新
        refreshDataAnim();
    }

    /**
     * 拼装数据
     * @param pageNum
     */
    /*private void updateReqBean(int pageNum) {

        CarListReqBean reqBean = new CarListReqBean();
        reqBean.setLat(String.valueOf(location.getLatitude()));
        reqBean.setLng(String.valueOf(location.getLongitude()));
        reqBean.setCityCode(userCityEntity == null ? "-1" : userCityEntity.getCityCode());
        reqBean.setPageNum(String.valueOf(pageNum));
        getCarList(reqBean);
    }*/

    /**
     * 删选条件拼装数据
     *
     * @param pageNum
     */
    private void updateReqBean(int pageNum) {
        /*CarListReqBean reqBean = new CarListReqBean();
        reqBean.setCityCode(userCityEntity == null ? location.getCityCode() : userCityEntity.getCityCode());
        reqBean.setLat(userCityEntity == null ? String.valueOf(location.getLatitude()) : String.valueOf(userCityEntity.getLat()));
        reqBean.setLng(userCityEntity == null ? String.valueOf(location.getLongitude()) : String.valueOf(userCityEntity.getLon()));
        reqBean.setOnListing(carListReqBean.getOnListing() == null ? Constants.BLANK : carListReqBean.getOnListing());
        reqBean.setStateOnline(carListReqBean.getStateOnline() == null ? Constants.BLANK : carListReqBean.getStateOnline());
        reqBean.setIsCharging(carListReqBean.getIsCharging() == null ? Constants.BLANK : carListReqBean.getIsCharging());
        reqBean.setSocNumber(carListReqBean.getSocNumber() == null ? Constants.BLANK : carListReqBean.getSocNumber());*/

        if(userCityEntity != null){
            carListReqBean.setCityCode(userCityEntity.getCityCode());
            carListReqBean.setLat(String.valueOf(userCityEntity.getLat()));
            carListReqBean.setLng(String.valueOf(userCityEntity.getLon()));
        }else {
            carListReqBean.setCityCode(location.getCityCode());
            carListReqBean.setLat(String.valueOf(location.getLatitude()));
            carListReqBean.setLng(String.valueOf(location.getLongitude()));
        }
        carListReqBean.setPageNum(String.valueOf(pageNum));

        getCarList(carListReqBean);
    }

    public void getCarList(final CarListReqBean reqBean) {

//        RequestOptService.getCarListPageNum(CarListActivity.this, reqBean, getCarListListener, getCloseSwipRefreshErrorListener());
        RequestOptService.getCarListPageNum(getApplicationContext(), reqBean, new Response.Listener<NetPageInfoBeanWrapper<CarDetailEntity>>() {
            @Override
            public void onResponse(NetPageInfoBeanWrapper<CarDetailEntity> response) {
                closeSwipRefreshAnim();
                if (handleResponse(response)) {
                    return;
                }
                carRespBean = response.getPageInfo();

                mainCityNameTotalCountTv.setText( "(" + response.getModelData().getTotalCount() + ")");
                mainCarCount.setText(response.getModelData().getCount() + "台");

                handleLoadMore(carRespBean);
            }
        },getCloseSwipRefreshErrorListener());
    }


    @Override
    public void onRefresh() {
        updateReqBean(1);
    }

    @Override
    protected void loadMoreData() {
        updateReqBean(carRespBean.getPageNum() + 1);
    }

    @OnItemClick(R.id.list)
    public void onItemClick(int position) {
        CarDetailEntity carDetailEntity = adapter.getItem(position);
        Intent controlIntent = new Intent(CarListActivity.this,StartDealActivity.class);
        controlIntent.putExtra(Constants.BUNDLE_DATA,carDetailEntity.getCarId());
        startActivity(controlIntent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (ReqConstants.REQUEST_CODE_FOR_SELECT_CITY == requestCode) {
                //选择城市
                userCityEntity = data.getParcelableExtra(com.fqcar.fqdf.util.Constants.Tag.CITY_ENTITY);
                if (userCityEntity == null) {
                    mainCityNameTv.setText(location.getCity());
                } else {
//                    com.fqcar.fqdf.util.Constants.userCityCode = userCityEntity.getCityCode();
                    mainCityNameTv.setText(userCityEntity.getName());
                    carListReqBean = new CarListReqBean();
                    updateReqBean(1);
                }
            } else if (ReqConstants.REQUEST_CODE_FOR_SELECT_CONDITION == requestCode) {
                carListReqBean = new CarListReqBean();
                carListReqBean = data.getParcelableExtra(com.fqcar.fqdf.util.Constants.Tag.SELECT_CONDITION);
                if(userCityEntity != null){
                    carListReqBean.setCityCode(userCityEntity.getCityCode());
                    carListReqBean.setLat(String.valueOf(userCityEntity.getLat()));
                    carListReqBean.setLng(String.valueOf(userCityEntity.getLon()));
                }else {
                    carListReqBean.setCityCode(location.getCityCode());
                    carListReqBean.setLat(String.valueOf(location.getLatitude()));
                    carListReqBean.setLng(String.valueOf(location.getLongitude()));
                }
                updateReqBean(1);
//                getCarList(carListReqBean);
            }
        }
    }

    @OnClick({R.id.main_back_iv, R.id.main_city_name_layout, R.id.main_search_btn, R.id.main_select_condition_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_back_iv:
//                finish();
                Intent intent = new Intent(CarListActivity.this, NormalHomeActivity.class);
                Bundle bundle = new Bundle();
                if(userCityEntity != null){
                    bundle.putParcelable(com.fqcar.fqdf.util.Constants.BUNDLE_DATA, userCityEntity);
                }
                if(carListReqBean != null){
                    bundle.putParcelable(com.fqcar.fqdf.util.Constants.BUNDLE_DATA_EXT, carListReqBean);
                }
                if(!bundle.isEmpty()){
                    intent.putExtras(bundle);
                    intent.setAction(com.fqcar.fqdf.util.Constants.Action.HOME_SELECT_CONDITION_ACTION);
                }
                CarListActivity.this.startActivity(intent);
                finish();
                break;
            case R.id.main_city_name_layout:
                Intent intentCity = new Intent(this, CityListActivity.class);
                startActivityForResult(intentCity, ReqConstants.REQUEST_CODE_FOR_SELECT_CITY);
                break;
            case R.id.main_search_btn:
                Intent searchIntent = new Intent(CarListActivity.this,SearchCarActivity.class);
                startActivity(searchIntent);
                break;
            case R.id.main_select_condition_btn:
                Intent intentCondition = new Intent(this, SelectConditionActivity.class);
                Bundle bundleCondition = new Bundle();
                bundleCondition.putParcelable(Constants.BUNDLE_DATA, carListReqBean);
                intentCondition.putExtras(bundleCondition);
                startActivityForResult(intentCondition, ReqConstants.REQUEST_CODE_FOR_SELECT_CONDITION);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(CarListActivity.this, NormalHomeActivity.class);
        Bundle bundle = new Bundle();
        if(userCityEntity != null){
            bundle.putParcelable(com.fqcar.fqdf.util.Constants.BUNDLE_DATA, userCityEntity);
        }
        if(carListReqBean != null){
            bundle.putParcelable(com.fqcar.fqdf.util.Constants.BUNDLE_DATA_EXT, carListReqBean);
        }
        if(!bundle.isEmpty()){
            intent.putExtras(bundle);
            intent.setAction(com.fqcar.fqdf.util.Constants.Action.HOME_SELECT_CONDITION_ACTION);
        }
        CarListActivity.this.startActivity(intent);
        finish();
    }
    /*@Override
    public void onItemClick(View view, int position) {
        CarDetailEntity carDetailEntity = adapter.getItem(position);
        Intent controlIntent = new Intent(CarListActivity.this,StartDealActivity.class);
        controlIntent.putExtra(Constants.BUNDLE_DATA,carDetailEntity.getCarId());
        startActivity(controlIntent);
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }*/
}
